/*
 * Copyright 2008 Hector Francisco Hernandez
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

var buildingArray = new Array();
var buildingLeftGorillaX;
var buildingLeftGorillaY;
var buildingRightGorillaX;
var buildingRightGorillaY;

function buildingInit(canvas)
{
  buildingCreate(canvas);
  buildingGenerateGorillasPositions();
}

function buildingCreate(canvas)
{
  buildingsHTML = "";

  for (i = 0; i < 10; i++) {
    x = i == 0 ? 1 : x + width + 1;
    y = 480 - 240 * Math.random();
    width = Math.floor((640 - x) / (10 - i) *
        (i == 9 ? 1 : 0.8 + 0.4 * Math.random()));
    height = 480 - y;

    building = new Object();
    building.x = x;
    building.y = y;
    building.width = width;
    building.height = height;
    buildingArray.push(building);

    randomColor = Math.random();
    colorHex = randomColor < 1/3 ?
        "#008B8B" : (randomColor < 2/3 ? "#999999" : "#8B0000");
    buildingsHTML += buildingDrawRect(x, y, width, height, colorHex);

    // draw windows
    winx = x + 10;
    limx = x + width - 2;

    do {
      winy = y + 10;

      while (winy < 480) {
        buildingsHTML += buildingDrawRect(winx - 3, winy - 5, 4, 6,
            Math.random() < 1/3 ? "#4D4D4D" : "#FFFF00");
        winy += 15;
      }

      winx += 10;
    } while (winx < limx);

  }

  canvas.innerHTML += buildingsHTML;

}

function buildingDrawRect(left, top, width, height, color)
{
  return "<div style=\"position:absolute; left:" + left + "px; top:" + top +
      "px; width:" + width + "px; height:" + height + "px; background:" +
      color + "\"><!-- --></div>";
}

function buildingGenerateGorillasPositions()
{
  n = Math.floor(Math.random() * 3);

  buildingLeftGorillaX = Math.floor(buildingArray[n].x +
      (buildingArray[n].width - 20) * Math.random());
  buildingLeftGorillaY = buildingArray[n].y - 21;

  n = Math.floor(Math.random() * 3) + 7;

  buildingRightGorillaX = Math.floor(buildingArray[n].x +
      (buildingArray[n].width - 20) * Math.random());
  buildingRightGorillaY = buildingArray[n].y - 21;
}

function buildingGetLeftGorillaPosition()
{
  return [buildingLeftGorillaX, buildingLeftGorillaY];
}

function buildingGetRightGorillaPosition()
{
  return [buildingRightGorillaX, buildingRightGorillaY];
}

function buildingIsIn(x, y)
{
  for (n in buildingArray) {
    bx = buildingArray[n].x;
    bx1 = buildingArray[n].x + buildingArray[n].width;
    by = buildingArray[n].y;

    if (x > bx  && x < bx1 && y > by) {
      return true;
    }
  }

  return false;
}

